package cn.jasonone.jame.model;

import cn.hutool.core.util.StrUtil;
import cn.jasonone.jame.FontUtil;
import javafx.geometry.VPos;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import lombok.Getter;
import lombok.Setter;

/**
 * 文本便签
 * @author Jason
 * @since 1.0.0
 */
public class GLabel extends Spirit {
    @Getter
    private String text;
    /**
     * 是否绘制空心文字
     */
    private boolean stroke;
    private double padding;
    @Setter
    @Getter
    private double maxWidth;

    public GLabel() {
        this("");
    }

    public GLabel(String text, Object... params) {
        this.setText(text, params);
    }


    public GLabel(boolean stroke, String text, Object... params) {
        this(text, params);
        this.stroke = stroke;
    }

    /**
     * 是否绘制空心文字
     *
     * @param stroke {@code true or false}
     */
    public void setStroke(boolean stroke) {
        this.stroke = stroke;
    }

    @Override
    public void draw(GraphicsContext gc) {
        if (this.isVisible()) {
            gc.setTextBaseline(VPos.TOP);
            gc.setFont(getFont());
            if (maxWidth > 0) {
                if (stroke) {
                    gc.setStroke(getStrokeColor());
                    gc.strokeText(text, getX() + padding / 2, getY() + padding / 2, maxWidth);
                } else {
                    gc.setFill(getFillColor());
                    gc.fillText(text, getX() + padding / 2, getY() + padding / 2, maxWidth);
                }
            } else {
                if (stroke) {
                    gc.setStroke(getStrokeColor());
                    gc.strokeText(text, getX() + padding / 2, getY() + padding / 2);
                } else {
                    gc.setFill(getFillColor());
                    gc.fillText(text, getX() + padding / 2, getY() + padding / 2);
                }
            }
        }

    }

    public void setText(String text, Object... params) {
        this.text = StrUtil.format(text, params);
        Dimension dimension = FontUtil.getDimension(getFont(), this.text);
        this.setSize(dimension.getWidth() + padding, dimension.getHeight() + padding);
    }

    @Override
    public void setFont(Font font) {
        super.setFont(font);
        Dimension dimension = FontUtil.getDimension(getFont(), this.text);
        this.setSize(dimension.getWidth() + padding, dimension.getHeight() + padding);
    }

    @Override
    public void update() {

    }

}
